package cn.com.nes.site.controller.system;

import cn.com.nes.mybatis.agent.system.entity.TUser;
import cn.com.nes.site.controller.system.vo.QueryInstallUserVO;
import cn.com.nes.site.controller.system.vo.QueryRepairUserVO;
import cn.com.nes.site.controller.system.vo.RepairUserVO;
import cn.com.nes.site.entity.PageData;
import cn.com.nes.site.entity.vo.BaseVO;
import cn.com.nes.site.entity.vo.system.GetAuthTokenByErUserIdVO;
import cn.com.nes.site.entity.vo.system.GetAuthTokenVO;
import cn.com.nes.site.entity.vo.system.GetOtherInstallUserVO;
import cn.com.nes.site.entity.vo.system.UpdateUserInfoFromEasyRepairVO;
import cn.com.nes.site.entity.vo.user.ErUserQueryVO;
import cn.com.nes.site.entity.vo.user.PushUserInfoVO;
import cn.com.nes.site.entity.vo.user.QueryInstallUserSelectVO;
import cn.com.nes.site.service.system.company.BusinessCompanyService;
import cn.com.nes.site.service.system.user.SystemUserService;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 业务人员接口
 */
@Api(tags = {"系统/用户管理"})
@Slf4j
@Controller
@RequestMapping("/v2/system/user")
public class SystemUserController {

    @Resource
    private SystemUserService systemUserService;
    @Resource
    private BusinessCompanyService businessCompanyService;

    /**
     * 易修用户，查询负责的行政区划代码
     * @param vo
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "getErUserAreaCode", method = RequestMethod.GET)
    @ResponseBody
    public PageData getErUserAreaCode(@Validated ErUserQueryVO vo) throws Exception {
        List<String> areaCodes = systemUserService.getUserAreaCodes(vo.getErUserId());
        return PageData.ok("areaCodes", areaCodes);
    }

    /**
     * 查询安装人员下拉选
     * @param vo
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "getInstallUserSelect", method = RequestMethod.GET)
    @ResponseBody
    public PageData getInstallUserSelect(@Validated QueryInstallUserSelectVO vo) throws Exception {
        List<TUser> users = systemUserService.getInstallUserSelect(vo.getCompanyOrDepartmentId());
        return PageData.ok("installUserSelect", users);
    }

    /**
     * 查询安装人员列表
     * @param vo
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "getInstallUsersOfProject", method = RequestMethod.GET)
    @ResponseBody
    public PageData getInstallUsersOfProject(@Validated QueryInstallUserVO vo) throws Exception {
        return PageData.ok("installUsers", systemUserService.getInstallUsers(vo.convertToBo()));
    }
    /**
     * 查询维修人员列表
     * @param vo
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "getRepairUsers", method = RequestMethod.GET)
    @ResponseBody
    public PageData getRepairUsers(@Validated RepairUserVO vo) throws Exception {
        return PageData.ok("repairTechnicianList", systemUserService.getRepairUsers(vo.convertToBo()));
    }

    @RequestMapping(value = "getPcRepairUsers", method = RequestMethod.GET)
    @ResponseBody
    public PageData getPcRepairUsers(@Validated QueryRepairUserVO vo) throws Exception {
        return PageData.ok("RepairUsers", systemUserService.getPcRepairUsers(vo.convertToBo()));
    }
    /**
     * 查询安装人员下拉选 - 本部门
     * @param vo
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getOtherInstallUserSelectByDept", method = RequestMethod.GET)
    @ResponseBody
    public PageData getOtherInstallUserSelectByDept(GetOtherInstallUserVO vo) throws Exception {
        return PageData.ok("installUserSelect", systemUserService.getOtherInstallUserSelectByDept(vo));
    }

    /**
     * 批量初始化用户密码
     * @param vo
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "batchInitUserPassword", method = RequestMethod.GET)
    @ResponseBody
    public PageData batchInitUserPassword(BaseVO vo) throws Exception {
        return systemUserService.batchInitUserPassword();
    }

    /**
     * 推送热链云用户信息到易修和长效管护
     * @param vo
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/pushUserInfo", method = RequestMethod.POST)
    @ResponseBody
    public PageData pushUserInfo(@RequestBody PushUserInfoVO vo) throws Exception {
        String callCenterCompanyId = vo.getCallCenterCompanyId();
        String companyId = vo.getCompanyId();
        if (StrUtil.isBlank(callCenterCompanyId)) {
            return PageData.error("客服中心id不能为空!");
        }
        if (StrUtil.isBlank(companyId)) {
            return PageData.error("热链云公司id不能为空!");
        }

        // 绑定热链云公司和长效管护客服中心
        businessCompanyService.bindCompanyId(companyId,callCenterCompanyId);

        // 获取全部用户信息
        List<String> userIdList = systemUserService.getUserIdList(companyId);

        if (CollectionUtil.isEmpty(userIdList)) {
            return PageData.ok("当前公司没有用户，不用推送");
        }
        // 推送创建用户信息
        for (String userId : userIdList) {
            PushUserInfoVO pushUserInfoVO = new PushUserInfoVO();
            pushUserInfoVO.setUuid(userId);
            pushUserInfoVO.setCompanyId(companyId);
            pushUserInfoVO.setCallCenterCompanyId(callCenterCompanyId);

            systemUserService.pushUserInfo(pushUserInfoVO);
        }

        return PageData.ok("用户信息推送完成");
    }


    /**
     * 获取权限Token
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "getAuthToken", method = RequestMethod.GET)
    @ResponseBody
    public PageData getAuthToken(@Validated GetAuthTokenVO vo) throws Exception {
        return systemUserService.getAuthToken(vo);
    }

    /**
     * 获取权限Token(通过易修用户)
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "getAuthTokenByErUserId", method = RequestMethod.GET)
    @ResponseBody
    public PageData getAuthTokenByErUserId(@Validated GetAuthTokenByErUserIdVO vo) throws Exception {
        return systemUserService.getAuthTokenByErUserId(vo);
    }

    /**
     * 校验本部门是否已有施工负责人
     */
    @RequestMapping(value = "/checkHaveForeman", method = RequestMethod.POST)
    @ResponseBody
    public PageData checkHaveForeman(@RequestParam String departmentId) throws Exception {
        return PageData.ok("haveForeman", systemUserService.checkHaveForeman(departmentId));
    }

    /**
     * 校验当前登录人是否是施工队负责人
     */
    @RequestMapping(value = "/checkIsForeman", method = RequestMethod.GET)
    @ResponseBody
    public PageData checkIsForeman(@RequestParam String easyRepairUserId) throws Exception {
        return PageData.ok("isForeman", systemUserService.checkIsForeman(easyRepairUserId));
    }

    /**
     * 同步易修用户账号信息
     */
    @RequestMapping(value = "/updateUserInfoFromEasyRepair", method = RequestMethod.POST)
    @ResponseBody
    public PageData updateUserInfoFromEasyRepair(@RequestBody @Validated UpdateUserInfoFromEasyRepairVO vo) throws Exception {
        return PageData.ok("success", systemUserService.updateUserInfoFromEasyRepair(vo));
    }

}
